package it.infodea.tapestrydea.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.tapestry5.StreamResponse;
import org.apache.tapestry5.services.Response;

public class ByteArrayStreamResponse implements StreamResponse {

	private ByteArrayOutputStream bos;
	private String contentType;
	
	private ByteArrayStreamResponse(ByteArrayOutputStream bos, String contentType) {
		super();
		this.bos = bos;
		this.contentType = contentType;
	}

	public static StreamResponse getStream(byte[] bytes, String contentType){
		return new ByteArrayStreamResponse(new ByteArrayOutputStream(), contentType);
	}
	
	public static StreamResponse getStream(ByteArrayOutputStream bos, String contentType){
		return new ByteArrayStreamResponse(bos, contentType);
	}
	
	public String getContentType() {
		return contentType;
	}

	public InputStream getStream() throws IOException {
		return new ByteArrayInputStream(bos.toByteArray());
	}

	public void prepareResponse(Response response) {}
		
}
